/* eslint-disable semi */
/* eslint-disable no-control-regex */

//批量执行页面注入脚本
export const executeScripts = function (tabId, injectDetailsArray, callback, message) {
  console.log('%cexecuteScript message', 'color: blue; font-size: 18px', message, injectDetailsArray)
  function createCallback(tabId, injectDetails, innerCallback) {
    return function () {
      chrome.tabs.executeScript(tabId, injectDetails, innerCallback)
    }
  }
  for (var i = injectDetailsArray.length - 1; i >= 0; --i)
    callback = createCallback(tabId, injectDetailsArray[i], callback);


  if (callback !== null)
    callback();
}


//发布广告填充元素
export const publishJobadFill = function(tabId, publishadinfo, channelJs) {
  var adDetail = JSON.stringify(publishadinfo['adDetail'])
  console.log('======================')
  console.log('adDetail: >>', adDetail)
  console.log('======================')
  adDetail = adDetail.replace(/\u0008|\u2028/g, '')
  console.log(JSON.parse(adDetail))
  //其他渠道webSiteMem是json格式，escape需要注意
  var jscode = "var webSiteMem = escape(" + JSON.stringify(publishadinfo['webSiteMem']) + ");"
  //51和智联webSiteMem来源不同是，需要单独处理，相应的在远程js里只有51有用到
  if (publishadinfo["channelId"] == 1 || publishadinfo["channelId"] == 2) {
    jscode = "var webSiteMem =escape(\"" + publishadinfo['webSiteMem'] + "\");"
  }
  executeScripts(tabId, [{
    file: "js/libs/jquery-1.11.3.min.js"
  }, {
    code: jscode = jscode +
          "var userLogin = escape(\"" + publishadinfo['webSiteUsername'] + "\");" +
          "var userPassword = escape(\"" + publishadinfo['webSiteUserPwd'] + "\");" +
          "var isNewAccount =" + publishadinfo['isNewAccount'] + ";" +
          "var taskId ='" + publishadinfo['taskId'] + "';" +
          "var adDetail =" + adDetail + ";" +
          "var adBaseInfo =" + JSON.stringify(publishadinfo['adBaseInfo']) + ";" +
          "var extensionId ='" + publishadinfo['extensionId'] + "';" +
          "var tabId =" + tabId + ";"+
          `var publishAdInfo = ${JSON.stringify(publishadinfo)}`
  }, {
    code: channelJs
  }])
}